package spring.boot.client.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import spring.boot.client.codec.Invocation;
import spring.boot.client.common.NettyClientConstant;

@Slf4j
@Service
public class NettyMessageService {

    /**
     * 发送消息
     *
     * @param invocation 消息体
     */
    public void send(Invocation invocation) {
        if (NettyClientConstant.CLIENT_CHANNEL == null) {
            log.error("[send][连接不存在]");
            return;
        }
        if (!NettyClientConstant.CLIENT_CHANNEL.isActive()) {
            log.error("[send][连接({})未激活]", NettyClientConstant.CLIENT_CHANNEL.id());
            return;
        }

        log.info("Netty客户端发送消息[{}]", invocation);
        // 发送消息
        NettyClientConstant.CLIENT_CHANNEL.writeAndFlush(invocation);
    }

}
